package top.xiaoshuaila.moments.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import top.xiaoshuaila.moments.pojo.Users;

import java.sql.Timestamp;
import java.util.List;

@Repository
public interface UserMapper extends BaseMapper<Users> {
    @Select("select \n" +
            "  *,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where users.id = focus.userid) as focus,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where users.id = focus.followerid) as focuto,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    message \n" +
            "  where message.userid = users.id \n" +
            "    and message.isread = 0) as nrmes \n" +
            "from\n" +
            "  users \n" +
            "where users.username = '${username}' ")
    Users getWithName(String username);

    @Select("select count(*)>0 from users where users.username='${un}'")
    boolean containusername(String un);

    @Insert("insert into users (username,realname,password,introduction,headphoto,time)values " +
            "('${un}','${rn}','${pw1}','${intro}','${headphoto}','${datetime}')")
    void regist(String un, String rn, String pw1, String intro, String headphoto, Timestamp datetime);

    @Insert("insert into focus(userid,followerid,time) values (${id},1,'${datetime}');")
    void adminfans(long id,Timestamp datetime);
    @Select("select users.id from users where users.username='${un}'")
    long getId(String un);
}
